package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.GoodsCategoryDTO;
import com.hxzy.entity.TbNewbeeMallAdminUser;
import com.hxzy.entity.TbNewbeeMallGoodsCategory;
import com.hxzy.mapper.TbNewbeeMallGoodsCategoryMapper;
import com.hxzy.service.TbNewbeeMallGoodsCategoryService;
import com.hxzy.util.SessionUtil;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import com.hxzy.vo.ThreeLevelCategoryVO;
import org.apache.catalina.manager.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class TbNewbeeMallGoodsCategoryServiceImpl implements TbNewbeeMallGoodsCategoryService {
    @Autowired
    private TbNewbeeMallGoodsCategoryMapper tbNewbeeMallGoodsCategoryMapper;


    @Override
    public BootstrapTableVO search(GoodsCategoryDTO goodsCategoryDTO) {
        PageHelper.startPage(goodsCategoryDTO.getPageNumber(), goodsCategoryDTO.getPageSize());
        List<TbNewbeeMallGoodsCategory> search = this.tbNewbeeMallGoodsCategoryMapper.search(goodsCategoryDTO);

        BootstrapTableVO tableVO=new BootstrapTableVO(new PageInfo(search));

        return tableVO;
    }

    @Override
    public ResultVO addOrUpdate(TbNewbeeMallGoodsCategory tbNewbeeMallGoodsCategory) {
        ResultVO resultVO=null;


        tbNewbeeMallGoodsCategory.setUpdateTime(new Date());
        TbNewbeeMallAdminUser user = SessionUtil.getUser();
        tbNewbeeMallGoodsCategory.setUpdateUser(user.getAdminUserId());

        int count=0;
        if (tbNewbeeMallGoodsCategory.getCategoryId()==null) {
            tbNewbeeMallGoodsCategory.setCreateTime(new Date());
            tbNewbeeMallGoodsCategory.setIsDeleted(new Byte("0"));
            tbNewbeeMallGoodsCategory.setCreateUser(user.getAdminUserId());
            count=this.tbNewbeeMallGoodsCategoryMapper.insert(tbNewbeeMallGoodsCategory);

        }else{
            count=this.tbNewbeeMallGoodsCategoryMapper.updateByPrimaryKeySelective(tbNewbeeMallGoodsCategory);
        }

        if (count>0){
            resultVO = ResultVO.success(ResultEnum.SUCDESS.getMessage());
        }else{
            resultVO=ResultVO.failed(ResultEnum.PARAMS_FAILED);
        }
        return resultVO;
    }

    @Override
    public ResultVO searchAll() {
        ResultVO resultVO = null;

        List<ThreeLevelCategoryVO> arr = new ArrayList<>();

        //查询所有的数据,循环第一级
        List<TbNewbeeMallGoodsCategory> dbList = this.tbNewbeeMallGoodsCategoryMapper.searchAll();
        //循环第一级
        for (TbNewbeeMallGoodsCategory one : dbList) {
            if (one.getParentId().intValue() == 0) {
                ThreeLevelCategoryVO newOne = new ThreeLevelCategoryVO();
                //把符合条件one的数据放到  newOne 中去
                newOne.setCategoryId(one.getCategoryId());
                newOne.setCategoryName(one.getCategoryName());
                //加载二级
                loadTwoCategoryLevel(newOne, dbList);
                arr.add(newOne);
            }

        }

        resultVO=ResultVO.success(arr);
        return resultVO;
    }

    //加载第二级分类
    private void   loadTwoCategoryLevel(ThreeLevelCategoryVO  parent,List<TbNewbeeMallGoodsCategory> dbList){
        List<ThreeLevelCategoryVO> arr = new ArrayList<>();

        for (TbNewbeeMallGoodsCategory gc : dbList) {
            if (gc.getParentId().intValue() == parent.getCategoryId()) {
                ThreeLevelCategoryVO child = new ThreeLevelCategoryVO();
                //把符合条件one的数据放到  newTwo 中去
                child.setCategoryId(gc.getCategoryId());
                child.setCategoryName(gc.getCategoryName());
                //加载三级
                loadThreeCategoryLevel(child, dbList);
                arr.add(child);
            }
        }
        //有子节点
        if(arr.size()>0){
            parent.setChildren(arr);
        }
    }

    //加载第三级分类
    private void   loadThreeCategoryLevel(ThreeLevelCategoryVO  parent,List<TbNewbeeMallGoodsCategory> dbList){
        //二级分类有还是没有
        List<ThreeLevelCategoryVO>  arr=new ArrayList<>();
        //  where parent_id=15    ;
        for(TbNewbeeMallGoodsCategory  gc: dbList){
            if(gc.getParentId().intValue() == parent.getCategoryId().intValue() ){
                ThreeLevelCategoryVO  child=new ThreeLevelCategoryVO();
                //把符合条件one的数据放到  newOne 中去
                child.setCategoryId( gc.getCategoryId());
                child.setCategoryName(gc.getCategoryName());
                //你自己很清楚没有第4级
                //放到集合中
                arr.add(child);
            }
        }

        //有子节点
        if(arr.size()>0){
            parent.setChildren(arr);
        }
    }

}
